﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Web.Cms.Repositories.Published;
using Proggy.Web.Cms.Cache;
using Proggy.Web.Cms.Membership;
using Proggy.Web.Cms.Context;
using Proggy.Web.Cms.ContentModel;
using System.Web;
using Proggy.Web.Cms.Seo.Service.Concrete;
using System.Configuration;
using Proggy.Web.Cms.Seo.Service.Abstract;

namespace Proggy.Web.Cms.Controllers.FrontEnd
{
    /// <summary>
    /// Controller from which all Content Model Controllers should inherit
    /// </summary>
    public abstract class FrontEndControllerBase : ProggyControllerBase
    {
        #region CONSTRUCTORS

        /// <summary>
        /// Constructor where CurrentContentModel is set using ProggyFrontEndRequestContext
        /// </summary>
        public FrontEndControllerBase()
        {
            // check current
            if (FrontEndRequestContext.Current == null) {
                FrontEndRequestContext.RefreshCurrent();
            }

            // set content
            this.CurrentContentModel = FrontEndRequestContext.Current.CurrentContentModel;

        }

        /// <summary>
        /// Constructor for testability - sets CurrentContentModel using parameter
        /// </summary>
        /// <param name="currentContentModel"></param>
        public FrontEndControllerBase(IContentModel currentContentModel)
        {
            // set
            this.CurrentContentModel = currentContentModel;
        }

        #endregion

        #region ACTION RESULTS

        /// <summary>
        /// Display view for current content model
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        [ProggyFrontEndAuthorize]
        public virtual ActionResult HandleUrl(int nodeId, bool asJson=false)
        {
            if (asJson) {
                return this.JsonHandleUrl(nodeId);
            }
            
            return this.ReturnViewForCurrentContentModel();
        }

        /// <summary>
        /// Return view and model for current content model
        /// </summary>
        /// <returns></returns>
        protected ActionResult ReturnViewForCurrentContentModel()
        {
            // check 
            this.CheckCurrentContentModel();

            //
            return View("~/Views/ProggyTemplates/" + this.CurrentContentModel.Template + ".cshtml", this.CurrentContentModel);
        }

        /// <summary>
        /// Return JSON for current content model
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        [ProggyFrontEndAuthorize]
        public ActionResult JsonHandleUrl(int nodeId)
        {
            return this.ReturnJsonForCurrentContentModel();
        }

        /// <summary>
        /// Return JSON for current content model
        /// </summary>
        /// <returns></returns>
        public ActionResult ReturnJsonForCurrentContentModel()
        {
            // check
            this.CheckCurrentContentModel();

            //
            return Json(this.CurrentContentModel.GetJsonObject(), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Checks if current content model exists - throws exception if not
        /// </summary>
        public void CheckCurrentContentModel()
        {
            if (this.CurrentContentModel == null)
            {
                throw new HttpException(404, "Controller.CurrentContentModel is null");
            }
        }

        #endregion

        #region PROPERTIES

        /// <summary>
        /// The current content model
        /// </summary>
        public IContentModel CurrentContentModel { get; set; }

        

        #endregion        
    }
}
